import 'package:boss/models/store_area.dart';
import 'package:boss/provider/table_provider.dart';
import 'package:boss/ui/page/tableManagement/page/areaManagement/page/pickerArea/picker_area_page.dart';
import 'package:boss/ui/page/tableManagement/page/tableDetail/provider/table_detail_provider.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/title_row_button.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class TableDetailContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: EdgeInsets.symmetric(
          horizontal: 30.w,
        ),
        child: Column(
          children: <Widget>[
            Consumer2<TableProvider, TableDetailProvider>(builder: (context, tableProvider, tableDetailProvider, _) {
              return TitleRowButton(
                title: '所属区域',
                hintText: '请选择',
                value: tableDetailProvider.storeTable?.areaName,
                onTap: () async {
                  final res = await Navigator.pushNamed(
                    context,
                    PickerAreaPage.pageName,
                    arguments: {'selectedAreaId': tableDetailProvider.storeTable?.areaId},
                  );
                  if (res != null) {
                    tableDetailProvider.setStoreArea(res);
                  }
                },
              );
            }),
            Consumer<TableDetailProvider>(builder: (context, tableDetailProvider, _) {
              return BaseTextFormField(
                prefixText: '桌台名称/编号',
                isRequired: true,
                hintText: '例如A08',
                initialValue: tableDetailProvider.storeTable.tableName,
                onChanged: (value) {
                  var tableDetailProvider = Provider.of<TableDetailProvider>(
                    context,
                    listen: false,
                  );
                  tableDetailProvider.setStoreTableName(value);
                },
              );
            }),
            Consumer<TableDetailProvider>(builder: (context, tableDetailProvider, _) {
              return BaseTextFormField(
                prefixText: '座位数',
                isRequired: true,
                hintText: '请输入',
                maxLength: 2,
                keyboardType: TextInputType.number,
                inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
                initialValue: '${tableDetailProvider.storeTable.maxPerson ?? ''}',
                onChanged: (value) {
                  var tableDetailProvider = Provider.of<TableDetailProvider>(
                    context,
                    listen: false,
                  );
                  try {
                    tableDetailProvider.setStoreTableSeatNumber(int.parse(value));
                  } catch (e) {
                    tableDetailProvider.setStoreTableSeatNumber(null);
                  }
                },
              );
            }),
          ],
        ),
      ),
    );
  }
}
